use crate::{process_sign_jwt, process_verify_jwt, CmdExecutor};
use clap::Parser;
use enum_dispatch::enum_dispatch;

#[derive(Debug, Parser)]
#[enum_dispatch(CmdExecutor)]
pub enum JwtSubCommand {
    #[command(name = "sign", about = "Sign a JWT")]
    Sign(JwtSignOpts),
    #[command(name = "verify", about = "Verify a JWT")]
    Verify(JwtVerifyOpts),
}

#[derive(Debug, Parser)]
pub struct JwtSignOpts {
    #[arg(long, default_value = "acme")]
    pub sub: String,
    #[arg(long, default_value = "device1")]
    pub aud: String,
    #[arg(long, default_value = "14d")]
    pub exp: String,
    #[arg(long, default_value = "secret")]
    pub secret: String,
}

#[derive(Debug, Parser)]
pub struct JwtVerifyOpts {
    #[arg(short, long, default_value = "")]
    pub token: String,
    #[arg(long, default_value = "secret")]
    pub secret: String,
}

impl CmdExecutor for JwtSignOpts {
    async fn execute(self) -> anyhow::Result<()> {
        process_sign_jwt(&self.sub, &self.aud, &self.exp, self.secret.as_bytes())?;
        Ok(())
    }
}

impl CmdExecutor for JwtVerifyOpts {
    async fn execute(self) -> anyhow::Result<()> {
        process_verify_jwt(&self.token, self.secret.as_bytes())?;
        Ok(())
    }
}
